#ifndef _TCP_SESSION_H
#define _TCP_SESSION_H
#include <uv.h>
#include <mutex>
#include <string>
#include "Session.h"
class CTCPSession;
typedef std::shared_ptr<CTCPSession> CTCPSessionPtr;
typedef std::function<void(CTCPSessionPtr session, char* szByte, int len)> MsgCallBack;
typedef std::function<void(int status)> ConnStatusCallBack;
typedef std::function<void()> CloseCallBack;
class CTCPSession :public std::enable_shared_from_this < CTCPSession >
{
public:
	CTCPSession(uv_loop_t* loop);
	~CTCPSession();
	bool Connect(char* ip, size_t port);
	void Write(char* buf, size_t bsize);
	void OnMsgCall(MsgCallBack msgcb);
	void OnConnStatus(ConnStatusCallBack msgcb){ _connStatus = msgcb; }
	void OnCloseCb(CloseCallBack closecb){ _closecb = closecb; };
	void SetReConnInteral(size_t ms){ _interanl_ms = ms ;}
	void CloseReConn(){ _interanl_ms =0;}
	void _OnCloseCb();
	void _OnConnectCb(uv_connect_t* req, int status);
	void _OnReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf);
	void _OnWriteCb(uv_write_t* req, int status);
	void _ReConnect(){Connect((char*)_perrip.c_str(),_perrport);}
private:
	void _AddReconnTimer();
private:
	uv_tcp_t  _socket ;
	uv_connect_t  _connect;
	bool _bconn = false;
	MsgCallBack _msgcb = nullptr;
	CloseCallBack _closecb = nullptr;
	ConnStatusCallBack _connStatus = nullptr;
	uv_loop_t* _loop = nullptr;
	size_t _interanl_ms=0;
	std::string _perrip;
	size_t	_perrport;
public:
	SBuf _rbuf;
};
#endif

